package com.zdb.demo.design.pattern.creational.singleton;

public class ReflectStaticInnerClassSingleton {

    /**
     * 利用静态内部类的锁，
     */
    private static class Inner {
        private static ReflectStaticInnerClassSingleton innerClassSingleton;

        static {
            innerClassSingleton = new ReflectStaticInnerClassSingleton();
        }
    }

    private ReflectStaticInnerClassSingleton() {
        if(Inner.innerClassSingleton != null) {
            throw new RuntimeException("单例类不能生成多个对象");
        }
        System.out.println("----------  init ReflectStaticInnerClassSingleton -----------");
    }

    /**
     * 因为类加载时会有锁，在1,2,3执行完成后才会释放锁，所以不用担心指令重排的问题
     * @return
     */
    public static ReflectStaticInnerClassSingleton getInstance() {
        return Inner.innerClassSingleton;
    }

    public static void test() {
        System.out.println("---------- test ------------");
    }
}
